#pragma once
#include <string>
#include <sys/stat.h>
#include <sys/time.h>
#include <ctime>
#include <boost/algorithm/string.hpp>

#include "../../logsystem/logger/log.h"
namespace ns_until
{
    const std::string tmp_path = "./tmp/";
    class PathUtil
    {
    public:
        static std::string AddSuffix(const std::string &file_name, const std::string &suffix)
        {
            return tmp_path + file_name + suffix;
        }
        // 123 ->123.cpp
        static std::string Src(const std::string &file_name)
        {
            return AddSuffix(file_name, ".cc");
        }
        static std::string Exe(const std::string &file_name)
        {
            return AddSuffix(file_name, ".exe");
        }
        static std::string CompilerError(const std::string &file_name)
        {
            return AddSuffix(file_name, ".compile_error");
        }

        // 运行时,需要的文件
        static std::string Stdin(const std::string &file_name)
        {
            return AddSuffix(file_name, ".stdin");
        }
        static std::string Stdout(const std::string &file_name)
        {
            return AddSuffix(file_name, ".stdout");
        }
        static std::string Stderr(const std::string &file_name)
        {
            return AddSuffix(file_name, ".stderr");
        }
    };

    class FileUtil
    {
    public:
        static bool IsExistsFile(const std::string &file_name)
        {
            struct stat st;
            if (!stat(file_name.c_str(), &st))
                return true;
            return false;
        }
        // 获得毫秒时间戳
        static std::string GetTimeMs()
        {
            struct timeval _time;
            gettimeofday(&_time, nullptr);
            return std::to_string(_time.tv_sec * 1000 + _time.tv_usec / 1000);
        }

        static std::string UniqFileName()
        {
            static std::atomic_uint id(0);
            id++;
            // 毫秒级时间戳+原子性递增唯一值: 来保证唯一性
            std::string ms = GetTimeMs();
            std::string uniq_id = std::to_string(id);
            return ms + "_" + uniq_id;
        }

        // 123.cpp ->123.cpp
        static bool WriteFile(const std::string &file_path, const std::string &code)
        {
            std::ofstream out(file_path);
            if (!out.is_open())
            {
                return false;
            }
            out.write(code.c_str(), code.size());
            out.close();
            return true;
        }

        // 到指定路径下读取文件,是否保存\n
        static bool ReadFile(const std::string &file_path, std::string *content, bool keep)
        {
            content->clear();
            std::ifstream in(file_path);
            if (!in.is_open())
            {
                return false;
            }
            // getline 默认删除\n
            std::string line;
            while (std::getline(in, line))
            {
                *content += line;
                if (keep)
                    *content += "\n";
            }
            return true;
        }

    private:
    };

    class StringUtil
    {
    public:
        /*************************************
         * str: 输入型，目标要切分的字符串
         * target: 输出型，保存切分完毕的结果
         * sep: 指定的分割符
         * **********************************/
        static void SplitString(const std::string &str, std::vector<std::string> *target, const std::string &sep)
        {
            // boost split
            boost::split((*target), str, boost::is_any_of(sep), boost::algorithm::token_compress_on);
        }
    };
}